//
//  RecommendViewController.m
//  Travel assistant
//
//  Created by 杨帆 on 16/3/10.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "RecommendViewController.h"
#import "HeardView.h"
#import "RmdStoryCell.h"
#import "RmdComplexCell.h"
#import "RmdSimpleCell.h"
#import "HeardModel.h"
#import "StoryModel.h"
#import "SelectionModel.h"
#import "LunBoDetailWebViewController.h"
#import "AllStoryViewController.h"
#import "DetaiViewController.h"
#import "StoryDetailViewController.h"
#import "SearchCollectionViewCell.h"
#import "HotCityModel.h"
#import "HotCityViewController.h"

#import "ScanningViewController.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height


// animation duration
static const CGFloat kAnimationDuration = 0.5;

@interface RecommendViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *heardArray;
@property (nonatomic,strong) HeardView *heardV;

@property (nonatomic,strong) NSMutableArray *storyArray;

@property (nonatomic,strong) NSMutableArray *selectionArray;

@property (nonatomic,strong) NSString *index;

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (nonatomic,assign) BOOL flag;

@property (nonatomic,strong) NSMutableArray *hotCityArray;

@property (nonatomic,strong) NSMutableArray *hotCityArray2;


@end

@implementation RecommendViewController

-(NSMutableArray *)heardArray{
    
    if (!_heardArray) {
        _heardArray = [NSMutableArray array];
    }
    return _heardArray;
}


-(NSMutableArray *)storyArray{
    
    if (!_storyArray) {
        
        _storyArray = [NSMutableArray array];
    }
    return _storyArray;
}

-(NSMutableArray *)hotCityArray{
    
    if (!_hotCityArray) {
        _hotCityArray = [NSMutableArray array];
    }
    return _hotCityArray;
}

-(NSMutableArray *)hotCityArray2{
    
    if (!_hotCityArray2) {
        _hotCityArray2 = [NSMutableArray array];
    }
    return _hotCityArray2;
}


-(void)viewWillAppear:(BOOL)animated{
    
    
    self.navigationController.navigationBar.hidden=NO;
    //显示tabbar
//    [self showTabBar];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];


    self.view.backgroundColor = [UIColor redColor];
    self.title = @"推荐";

    
    self.selectionArray = [NSMutableArray array];
    [self addTableView];

    [self makeDataSourceForCollectionView];

    [self makeDataSourceForTableView];
    
   
    
    [self creatCollectionView];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"热门城市" style:(UIBarButtonItemStylePlain) target:self action:@selector(HotCityAction:)];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(scanningAction:)];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"扫描" style:(UIBarButtonItemStylePlain) target:self action:@selector(scanningAction:)];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshDataSource)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
}
//轮播图 数据解析
-(void)makeDataSourceForHeard{
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    
    [manager GET:kUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *tempDictionary = responseObject;
        
        NSDictionary *tempDict = tempDictionary[@"data"];
        
        NSArray *tempArray = tempDict[@"elements"];
        
        NSDictionary *dictionary = tempArray[0];
        
        NSArray *array = dictionary[@"data"];
        
        for (NSDictionary *dict in array[0]) {
            
            HeardModel *hm = [[HeardModel alloc]initWithDictionary:dict];
            
            [self.heardArray addObject:hm];
            
            
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
           
            HeardModel *hm = [HeardModel new];
            
            for (int i = 0; i < self.heardArray.count; i++) {
                
                hm = self.heardArray[i];
                
                UIImageView *imageV = [UIImageView new];
                
                [imageV sd_setImageWithURL:[NSURL URLWithString:hm.image_url] placeholderImage:nil];
                
                
                imageV.frame = CGRectMake(kWidth*i, 0, kWidth, kHeight/3.3);
                
                imageV.userInteractionEnabled = YES;
                
                UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
                
                imageV.tag = 200+i;
                
                [imageV addGestureRecognizer:tap];
                [self.heardV.scrollView addSubview:imageV];
                
                
                
                
            }

            [self.tableView.mj_header endRefreshing];
            
            [self.tableView reloadData];
        });

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
        NSLog(@"失败错误信息：%@",error);
        
    }];
    
    
    
    
    
    
    
}
//collectionView 数据解析
-(void)makeDataSourceForCollectionView{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:kUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *tempDictionary = responseObject;
        
        NSDictionary *tempDict = tempDictionary[@"data"];
        
        NSArray *tempArray = tempDict[@"elements"];
        
        
        
        for (int i = 2; i < 6; i++) {
            
            NSDictionary *dict = tempArray[i];

            NSArray *array = dict[@"data"];
            
            for (NSDictionary *dic in array) {
                
               StoryModel *m = [StoryModel new];
                
                [m setValuesForKeysWithDictionary:dic];
                
                [self.storyArray addObject:m];
            }
    
        }
//        NSLog(@"%ld",self.storyArray.count);
        
        dispatch_async(dispatch_get_main_queue(), ^{

            
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
 
        });
  
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"错误信息为：%@",error);
    }];
    

}
/**
 *  tableView 数据解析
 */
-(void)makeDataSourceForTableView{
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:kUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *tempDictionary = responseObject;
        
        NSDictionary *tempDict = tempDictionary[@"data"];
        
        self.index = tempDict[@"next_start"];
        
        NSArray *tempArray = tempDict[@"elements"];
        
        for (int i = 7; i <= 46; i++) {
            
            SelectionModel *m = [SelectionModel new];
            NSDictionary *dict = tempArray[i];
            
            m.type = dict[@"type"];
            
            
            NSArray *array = dict[@"data"];
            
            for (NSDictionary *dic in array) {
                
                [m setValuesForKeysWithDictionary:dic];
                
                [self.selectionArray addObject:m];
                
                
            }
  
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
           
            [self.tableView.mj_header endRefreshing];
            
            [self.tableView reloadData];
            
        });
        
        
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
    
 
    
}

//下拉刷新

-(void)refreshDataSource{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self makeDataSourceForHeard];
        [self makeDataSourceForCollectionView];
        [self makeDataSourceForTableView];
        
    });
    

    
}

//上拉加载
-(void)loadMoreData{
    
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/index/?lat=40.0291541441076&lng=116.337305268566&next_start=%@&sign=39345396f40679c512f76b8d08adaf6a",self.index];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *tempDictionary = responseObject;
        
        NSDictionary *tempDict = tempDictionary[@"data"];
        
        self.index = tempDict[@"next_start"];
        NSArray *tempArray = tempDict[@"elements"];
        
       
        for (NSDictionary *tempDic in tempArray) {
            
            SelectionModel *m = [SelectionModel new];
            m.type= tempDic[@"type"];
            
            for (NSDictionary *dic in tempDic[@"data"]) {
                
                
                
                [m setValuesForKeysWithDictionary:dic];

                [self.selectionArray addObject:m];
            }
        }

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            
        });

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];

}

-(void)makeDataSourceForHotCity{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:kUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *tempDictionary = responseObject;
        
        NSDictionary *tempDict = tempDictionary[@"data"];
        
        NSMutableArray *tempArray = tempDict[@"search_data"];
        
        
        NSDictionary *dictionary = tempArray[0];
        
        for (NSDictionary *dic in dictionary[@"elements"]) {
            
            HotCityModel *m = [HotCityModel new];
            
            [m setValuesForKeysWithDictionary:dic];
            
            [self.hotCityArray addObject:m];
  
        }
        
        NSDictionary *dictionary1 = tempArray[1];
        
        for (NSDictionary *dic in dictionary1[@"elements"]) {
            
            HotCityModel *m = [HotCityModel new];
            
            [m setValuesForKeysWithDictionary:dic];
            
            [self.hotCityArray2 addObject:m];
            
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.collectionView reloadData];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"错误信息为:%@",error);
    }];
    
    
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        UIView *myView = [[UIView alloc] init];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 22)];
        
        titleLabel.text = @"每日精选故事";
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        [button setTitle:@"全部" forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(turnToAll:) forControlEvents:(UIControlEventTouchUpInside)];
        button.frame = CGRectMake(kWidth-40, 10, 30, 22);
        [myView addSubview:button];
        [myView addSubview:titleLabel];
        return myView;
 
    }else{
        
        UIView *myView = [[UIView alloc] init];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 200, 22)];
        titleLabel.text = @"精彩原创和专题";
        [myView addSubview:titleLabel];
        return myView;
        
        
        
    }
    
    
}


-(void)addTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor colorWithRed:0.95 green:1 blue:0.95 alpha:1];
    [self.view addSubview:self.tableView];
    
    
    self.heardV = [[HeardView alloc]initWithFrame:CGRectMake(0, 0,kWidth ,kHeight/3.3) ];
    
    self.tableView.tableHeaderView = self.heardV;
    
    [self makeDataSourceForHeard];
    //    [self.tableView registerClass:[RmdStoryCell class] forCellReuseIdentifier:@"storyCell"];
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 1;
    }else{
        
        return self.selectionArray.count;
    }
    
    
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    return 44;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section ==0) {
        return kHeight*0.75;
    }else{
        return kHeight*0.34;
    }
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        
        RmdStoryCell *cell = [[RmdStoryCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"storyCell"];
        
//        if (cell == nil) {
//            cell = [[RmdStoryCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"storyCell"];
//        }
//        
//        __weak __typeof(self) weakSelf = self;
        
        cell.myBlock = ^(NSString *spot_id){
            
            StoryDetailViewController *dvc = [StoryDetailViewController new];
            
            dvc.spot_id = spot_id;
            
            [self.navigationController pushViewController:dvc animated:YES];
            
        };
        
        
        [cell setData:self.storyArray];
        
        
        
        
        return cell;
        
    }else if (indexPath.section == 1) {
        
        SelectionModel *m = self.selectionArray[indexPath.row];
        
        
        
        if ([m.type intValue] == 4) {
            
        
            RmdComplexCell *cell = [[RmdComplexCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"ComplexCell"];
            
            if (cell == nil) {
                
                cell = [[RmdComplexCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"ComplexCell"];
                
            }
            
            
            cell.titleLabel.text = m.name;

            cell.dateLabel.text = m.first_day;
            
            cell.dayLabel.text = [NSString stringWithFormat:@"%ld 天",m.day_count];
            
            [cell.mainImageV sd_setImageWithURL:[NSURL URLWithString:m.cover_image] placeholderImage:[UIImage imageNamed:@"placehoder"]];
            
            cell.countLabel.text = [NSString stringWithFormat:@"%ld 浏览",m.view_count];
            
            cell.placeLabel.text = m.popular_place_str;
            
            [cell.userImageV sd_setImageWithURL:[NSURL URLWithString:m.user[@"avatar_s"]] placeholderImage:nil];
            
            cell.userNameLabel.text = [NSString stringWithFormat:@"by %@",m.user[@"name"]];
            
            
            return cell;
        }else if ([m.type intValue] == 7){
            
            
            RmdSimpleCell *cell = [[RmdSimpleCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"SimpleCell"];
            
            
            if (cell == nil) {
                
                cell = [[RmdSimpleCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"SimpleCell"];
            }
            
            
            [cell.mainImageV sd_setImageWithURL:[NSURL URLWithString:m.cover] placeholderImage:[UIImage imageNamed:@"placehoder"]];
            
            
            cell.cover_titleL.text = m.title;
            
            cell.cover_sub_titleL.text = [NSString stringWithFormat:@"「  %@  」",m.sub_title];

            return cell;
        }else if ([m.type intValue] == 12){
            
            RmdComplexCell *cell = [[RmdComplexCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"ComplexCell"];
            
            
            if (cell == nil) {
                
                cell = [[RmdComplexCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"ComplexCell"];
                
            }
            
            
            cell.titleLabel.text  = m.name;
            
            [cell.mainImageV sd_setImageWithURL:[NSURL URLWithString:m.cover_image] placeholderImage:[UIImage imageNamed:@"placehoder"]];
            
            cell.dateLabel.text = m.first_day;
            
            cell.dayLabel.text = [NSString stringWithFormat:@"%ld 故事",m.spot_count];
            
            cell.countLabel.text = [NSString stringWithFormat:@"%ld 浏览",m.view_count];
            
            
            [cell.userImageV sd_setImageWithURL:[NSURL URLWithString:m.user[@"avatar_s"]] placeholderImage:nil];
            
            cell.userNameLabel.text = [NSString stringWithFormat:@"by %@",m.user[@"name"]];

            return cell;
            
        }

    }
    
    return nil;
}

//跳转扫描二维码
-(void)scanningAction:(UIBarButtonItem *)sender{
    
    
    ScanningViewController *svc = [ScanningViewController new];
    
    [self.navigationController pushViewController:svc animated:YES];
    
    
}

//点击"全部"按钮 跳转
-(void)turnToAll:(UIButton *)sender{
    
    
    AllStoryViewController *svc = [AllStoryViewController new];

    //当push到下一个页面时隐藏tabbar
//    [self  setHidesBottomBarWhenPushed:YES];
    
    [self.navigationController pushViewController:svc animated:YES];
    
    
}


//点击轮播图跳转

-(void)tapAction:(UITapGestureRecognizer *)sender{

    HeardModel *m = [HeardModel new];
    
    m = self.heardArray[sender.view.tag - 200];
    
    
    LunBoDetailWebViewController *dvc = [LunBoDetailWebViewController new];
    
    dvc.webUrl = m.html_url;
    
    //当push到下一个页面时隐藏tabbar
//    [self  setHidesBottomBarWhenPushed:YES];
    
    [self.navigationController presentViewController:dvc animated:YES completion:^{
        
    }];
    

}

//显示tabbar
- (void)showTabBar{
    if (self.tabBarController.tabBar.hidden == NO)
    {
        return;
    }
    UIView *contentView;
    if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])
        
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    
    else
        
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
    self.tabBarController.tabBar.hidden = NO;
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    SelectionModel *m = self.selectionArray[indexPath.row];
    
    
    if ([m.type intValue] == 7) {
        
        
        LunBoDetailWebViewController *wvc = [LunBoDetailWebViewController new];
        
        wvc.webUrl = m.url;
        wvc.Title = m.title;
        [self presentViewController:wvc animated:YES completion:nil];
        
    }else if([m.type intValue] == 4){
        
        DetaiViewController *dvc = [DetaiViewController new];
        
        
        dvc.urlString = [m.ID stringValue];
        

        [self.navigationController pushViewController:dvc animated:YES];
        
        
        
    }else if ([m.type intValue] == 12){
        
    
        

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"敬请期待！" message:@"该故事暂未收录" preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:action];
        
        
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
    
    
    
}


-(void)creatCollectionView{
    
    
    self.layout = [[UICollectionViewFlowLayout alloc]init];
    self.layout.itemSize = CGSizeMake(0.26*KScreenWidth, 30);
    self.layout.minimumLineSpacing = 10;
    self.layout.minimumInteritemSpacing = 10;
    self.layout.sectionInset = UIEdgeInsetsMake(30,20,20,20);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:(CGRectMake(0, 64,KScreenWidth, KScreenHeight))collectionViewLayout:self.layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:248/255.0 green:245/255.0 blue:230/255.0 alpha:1];
    self.collectionView.contentSize = CGSizeMake(KScreenWidth, KScreenHeight) ;
    
    [self.collectionView registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:@"search"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MYheader"];
    self.collectionView.scrollEnabled=YES;
    self.collectionView.alwaysBounceVertical = YES;
    [self.view addSubview:self.collectionView];
    
    self.collectionView.hidden = YES;

    
    [self makeDataSourceForHotCity];
}


-(void)HotCityAction:(UIBarButtonItem *)sender{
    

    if (_flag == NO) {
        [self showCityView];
        
        sender.title = @"取消";
        _flag = YES;
    }else{

        [self dismissCityView];
        sender.title = @"热门城市";
        _flag = NO;
        
    }
    
    
}

- (void)showCityView {
    [self.collectionView setTop:-KScreenHeight];
    self.collectionView.hidden = NO;
    
    [UIView animateWithDuration:kAnimationDuration animations:^{
        [self.collectionView setTop:64];
    }];
}


- (void)dismissCityView {

    
    [UIView animateWithDuration:kAnimationDuration animations:^{
        [self.collectionView setTop:-KScreenHeight];
    }completion:^(BOOL finished){
        self.collectionView.hidden = YES;
    }];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if (section == 0) {
        
        return self.hotCityArray.count;
        
    }else{
        
        return self.hotCityArray2.count;
    }
    
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    SearchCollectionViewCell * cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"search" forIndexPath:indexPath];
    
    
    if (indexPath.section == 0) {
        
        HotCityModel *m = self.hotCityArray[indexPath.row];
        
        cell.cityLabel.text = m.name;
        
    }else{
        
        HotCityModel *m = self.hotCityArray2[indexPath.row];
        
        cell.cityLabel.text = m.name;
        
    }
    
    
    
    
    return cell;
}
#pragma mark --UICollectionViewDelegate

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotCityViewController *dvc = [HotCityViewController new];
    
    if (indexPath.section == 0) {
        
        HotCityModel *m = self.hotCityArray[indexPath.row];
        
        dvc.webUrl = [NSString stringWithFormat:@"http://web.breadtrip.com%@",m.url];
        
        dvc.name = m.name;
        
    }else{
        
        
        HotCityModel *m = self.hotCityArray2[indexPath.row];
        
        dvc.webUrl = [NSString stringWithFormat:@"http://web.breadtrip.com%@",m.url];
        dvc.name = m.name;
    }
    
    [self.navigationController pushViewController:dvc animated:YES];
    
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(KScreenWidth, 20);
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MYheader" forIndexPath:indexPath];
    UILabel *label = [[UILabel alloc]initWithFrame:(CGRectMake(10, 2, KScreenWidth, 30))];
    
    label.text = @"热门目的地";
    label.textColor = [UIColor colorWithRed:67/255.0 green:171/255.0 blue:182/255.0 alpha:1];
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    [header addSubview:label];
    UILabel *horizontalLineL = [[UILabel alloc]initWithFrame:(CGRectMake(10, 40, KScreenWidth-20, 2))];
    horizontalLineL.backgroundColor = [UIColor colorWithRed:67/255.0 green:171/255.0 blue:182/255.0 alpha:1];
    [header addSubview:horizontalLineL];
    
    if (indexPath.section ==1) {
        
        
        UILabel *horizontalLineL = [[UILabel alloc]initWithFrame:(CGRectMake(10, 0, KScreenWidth-20, 2))];
        horizontalLineL.backgroundColor = [UIColor colorWithRed:67/255.0 green:171/255.0 blue:182/255.0 alpha:1];
        [header addSubview:horizontalLineL];
    }
    
    
    return header;
    
}


@end
